//
//  GoodsSearchResultViewController.m
//  LoveHappyShopping
//
//  Created by 白水非泉 on 16/6/27.
//  Copyright © 2016年 白水非泉. All rights reserved.
//
#define RECELL @"RESULTCELL"

#import "GoodsSearchResultViewController.h"
#import <AFNetworking.h>
#import "GoodsResultCollectionViewCell.h"
#import "GoodsSearchResultModel.h"
#import "GoodsDetailViewController.h"
@interface GoodsSearchResultViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UISearchBarDelegate>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *resultArray;
@property(nonatomic,strong)UISearchBar *searchBar;
@end

@implementation GoodsSearchResultViewController
-(NSMutableArray *)resultArray{
    if (!_resultArray) {
        _resultArray=[NSMutableArray array];
    }
    return _resultArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavbar];
    self.edgesForExtendedLayout=UIRectEdgeNone;
    [self registCell];
    [self sendRequest];

    // Do any additional setup after loading the view from its nib.
}
-(void)setNavbar{
    UIButton *leftBtn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"btn_back"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backToForeView) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem=leftItem;
    UISearchBar *searchBar=[[UISearchBar alloc]init];
    self.searchBar=searchBar;
    searchBar.text=self.searchText;
    self.navigationItem.titleView=searchBar;
    self.view.backgroundColor=[UIColor lightGrayColor];
}
-(void)backToForeView{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)registCell{
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize=CGSizeMake(SCREENW*0.5-5, SCREENW*0.5+60);
    flowLayout.minimumLineSpacing=10;
    flowLayout.minimumInteritemSpacing=10;
    flowLayout.sectionInset=UIEdgeInsetsMake(10, 0, 0, 0);
    self.collectionView.collectionViewLayout=flowLayout;
    UINib *nib=[UINib nibWithNibName:@"GoodsResultCollectionViewCell" bundle:nil];
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:RECELL];
}
-(void)sendRequest{
    NSString *keywords=[self.searchText stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSString *urlStr=[NSString stringWithFormat:@"http://jkjby.yijia.com/jkjby/view/tmzk_api.php?keyword=%@&start=0&sort=s&price=0,999999",keywords];
    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
    [manager GET:urlStr parameters:nil success:^(NSURLSessionDataTask *task, NSDictionary *responseDict) {
        NSArray *listArray=responseDict[@"list"];
        for (NSDictionary *dict in listArray) {
            GoodsSearchResultModel *resultModel=[GoodsSearchResultModel resultModelWithDict:dict];
            [self.resultArray addObject:resultModel];
        }
        [self.collectionView reloadData];
    }
    failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
}
#pragma marks - collectionView的代理方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.resultArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    GoodsSearchResultModel *model=self.resultArray[indexPath.row];
    GoodsResultCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:RECELL forIndexPath:indexPath];
    cell.model=model;
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodsSearchResultModel *cellModel=self.resultArray[indexPath.row];
    GoodsDetailViewController *detailVC=[[GoodsDetailViewController alloc]init];
    detailVC.resultModel=cellModel;
    [self.navigationController pushViewController:detailVC animated:YES];
    self.hidesBottomBarWhenPushed=YES;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    return YES;
}
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if ([self searchBarShouldEndEditing:self.searchBar]) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
